<%-- 
    Document   : upload
    Created on : 14/07/2014, 04:36:05 PM
    Author     : Luis.Parada
--%>

<%@page import="java.io.File"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>

<%
    String filePath = "c:\\temp";
    File file;
    //out.print(request);
    //----------------------------------
    /*
     String cabecera = "";
     out.print("<table>");
     for (Enumeration param = request.getHeaderNames(); param.hasMoreElements();) {
     cabecera = (String) param.nextElement();
     out.print("<tr><td>");
     out.print(cabecera + "</td><td>");
     out.print(request.getHeader(cabecera) + "<br/>");
     out.print("</td></tr>");
     }
     */
    //----------------------------------
    if (ServletFileUpload.isMultipartContent(request)) {
        try {
            FileItemFactory f = new DiskFileItemFactory();

            ServletFileUpload upload = new ServletFileUpload(f);

            List items = upload.parseRequest(request);

            Iterator itemsIter = items.iterator();

            //Just for one file, to upload more than one file use while instead of if
            if (itemsIter.hasNext()) {
                FileItem fi = (FileItem) itemsIter.next();
                String fileName = fi.getName();

                String fileType = fileName.substring(fileName.lastIndexOf("."));
                if (fileType.equals(".png") || fileType.equals(".jpg") || fileType.equals(".jpeg")
                        || fileType.equals(".zip") || fileType.equals(".pdf")
                        || fileType.equals(".doc") || fileType.equals(".docx")
                        || fileType.equals(".xls") || fileType.equals(".xlsx")) {
                    /*
                     Date now = new Date();
                     SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH;mm;ss");                
                     fileName="myFile "+format.format(now)+fileName.substring(fileName.lastIndexOf("."));
                     */

                    if (fileName.lastIndexOf("\\") >= 0) {
                        file = new File(filePath
                                + fileName.substring(fileName.lastIndexOf("\\")));
                    } else {
                        file = new File(filePath + "\\"
                                + fileName.substring(fileName.lastIndexOf("\\") + 1));
                    }
                    fi.write(file);
                    out.print("Uploaded!");
                    //   Move File To Another Directory 
                    File afile = new File("c:\\temp\\" + fi.getName());
                    if (afile.renameTo(new File("C:\\temp2\\" + afile.getName()))) {
                        out.println("::::::::File is moved successful!");
                    } else {
                        out.println(":::::::::File is failed to move!");
                    }
                    //----------------------------------------
                } else {
                    out.print("Unsupported file type!");
                }
            }
        } catch (FileUploadException e) {
            out.print("Error FileUploadException: " + e);
        } catch (Exception e) {
            out.print(e.getMessage());
        }
    }
%>